package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.converter.VocChapterConverter;
import com.cskaoyan.wordmemorize.dao.entity.VocBookDO;
import com.cskaoyan.wordmemorize.dao.entity.VocChapterDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocBookMapper;
import com.cskaoyan.wordmemorize.dao.mapper.VocChapterMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteChapterOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocChapterDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.VocChapterCommand;
import com.cskaoyan.wordmemorize.request.VocChapterPageRequest;
import com.cskaoyan.wordmemorize.service.VocChapterService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class VocChapterServiceImpl extends ServiceImpl<VocChapterMapper, VocChapterDO> implements VocChapterService {

    @Resource
    VocChapterMapper chapterMapper;

    @Resource
    VocBookMapper vocBookMapper;

    @Resource
    VocChapterConverter vocChapterConverter;


    @Override
    public PageDTO<VocChapterDTO> getChapterPage(VocChapterPageRequest pageQuery) {


        Page<VocChapterDO> page = new Page<>();

        if (pageQuery.getPageNum() != null && pageQuery.getCount() != null) {

            page.setCurrent(pageQuery.getPageNum());
            page.setSize(pageQuery.getCount());
        }


        QueryWrapper<VocChapterDO> wrapper = new QueryWrapper<>();

        wrapper.eq("book_id", pageQuery.getBookId());
        wrapper.like(pageQuery.getKeyword() != null, "chapter_name", pageQuery.getKeyword());

        chapterMapper.selectPage(page, wrapper);

        List<VocChapterDO> records = page.getRecords();


        PageDTO<VocChapterDTO> vocChapterDTOPageDTO = vocChapterConverter.vocChapterDOs2Page(records, page.getTotal());


        return vocChapterDTOPageDTO;
    }

    @Transactional
    @Override
    public void addChapter(VocChapterCommand command) {

        chapterMapper.insert(vocChapterConverter.vocChapterCommand2DO(command));

        LambdaUpdateWrapper<VocBookDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(VocBookDO::getId, command.getBookId());
        wrapper.setSql("chapter_count  =  chapter_count + 1");
        vocBookMapper.update(wrapper);


    }

    @Transactional
    @Override
    public void updateChapter(VocChapterCommand command) {
        chapterMapper.updateById(vocChapterConverter.vocChapterCommand2DO(command));

    }

    @Override
    @Transactional
    public DeleteChapterOperateDTO deleteChapter(Long chapterId) {

        // 先获取章节对应信息，再进行删除
        VocChapterDO vocChapterDO = chapterMapper.selectById(chapterId);
        Long bookId = vocChapterDO.getBookId();
        chapterMapper.deleteById(chapterId);


        // 更新书籍的章节数
        LambdaUpdateWrapper<VocBookDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(VocBookDO::getId, bookId);
        wrapper.setSql("chapter_count = chapter_count - 1");

        vocBookMapper.update(wrapper);

        DeleteChapterOperateDTO deleteChapterOperateDTO = vocChapterConverter.vocChapterDO2DeleteDTO(vocChapterDO);

        // 返回值用于AOP记录日志
        return deleteChapterOperateDTO;
    }

    @Override
    @Transactional
    public List<DeleteChapterOperateDTO> deleteChapters(BatchDeleteCommand command) {

        List<VocChapterDO> vocChapterDOS = chapterMapper.selectByIds(command.getIds());

        for (VocChapterDO vocChapterDO : vocChapterDOS) {

            // 更新书籍的章节数
            LambdaUpdateWrapper<VocBookDO> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(VocBookDO::getId, vocChapterDO.getBookId());
            wrapper.setSql("chapter_count = chapter_count - 1");

            vocBookMapper.update(wrapper);

        }

        return vocChapterConverter.vocChapterDOs2DeleteDTOs(vocChapterDOS);
    }
}
